﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

namespace App_Code
{
    /// <summary>
    /// Summary description for TemplateLoader
    /// </summary>
    public static class TemplateLoader
    {
        const string MID_DIV = "<div id=\"divmid\">";
        const string RIGHT_DIV = "<div id=\"divright\">";

        static string HTMLIse(string text)
        {
            return HttpContext.Current.Server.HtmlEncode(text);
        }

        public static void Load(System.Web.UI.Page page)
        {
            try
            {
                string leftChunk;
                string rightChunk;
                Load2Internal(page, out leftChunk, out rightChunk);

                string realContentFileName = page.AppRelativeVirtualPath;
                realContentFileName = realContentFileName.Replace(".aspx", ".htm");
                realContentFileName = realContentFileName.Replace("~/", "");

                string szRealContent = page.Server.MapPath(realContentFileName);

                if (!File.Exists(szRealContent))
                {
                    page.Response.Write(HTMLIse("missing file " + realContentFileName));
                    return;
                }

                string content = GetFile(realContentFileName, page);

              //  content = content.Replace("é", "&eacute;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                int startOfContentMidTemplate = content.IndexOf(MID_DIV);
                if (startOfContentMidTemplate == -1)
                {
                    page.Response.Write(HTMLIse("content broken. Missing " + MID_DIV + " in file " + realContentFileName));
                    return;
                }

                int startOfContentRightTemplate = content.IndexOf(RIGHT_DIV);
                if (startOfContentRightTemplate == -1)
                {
                    page.Response.Write(HTMLIse("content broken. Missing " + RIGHT_DIV + " in file " + realContentFileName));
                    return;
                }

                string contentUsed = content.Substring(startOfContentMidTemplate, startOfContentRightTemplate - startOfContentMidTemplate);

                var sbret = new StringBuilder();
                sbret.Append(leftChunk);
                sbret.Append(contentUsed);
                sbret.Append(rightChunk);

                page.Response.Write(sbret);
            }
            catch (Exception ex)
            {
                page.Response.Write(HTMLIse("Templating failed! " + ex.Message));
            }
        }

        public static void Load2(System.Web.UI.Page page, out string leftChunk, out string rightChunk)
        {
            try
            {
                Load2Internal(page, out leftChunk, out rightChunk);
            }
            catch (Exception ex)
            {
                leftChunk = "";
                rightChunk = "";
                page.Response.Write(HTMLIse("Load Map failed! " + ex.Message));
            }
        }

        private static void Load2Internal(System.Web.UI.Page page, out string leftChunk, out string rightChunk)
        {
            try
            {
                string template = GetFile("template.htm", page);
                {
                    int startOfMidTemplate = template.IndexOf(MID_DIV);

                    if (startOfMidTemplate == -1)
                    {
                        throw new ApplicationException("template broken. Missing " + MID_DIV);
                    }
                    leftChunk = template.Substring(0, startOfMidTemplate);
                }

                int startOfmRightTemplate = template.IndexOf(RIGHT_DIV);

                if (startOfmRightTemplate == -1)
                {
                    throw new ApplicationException("template broken. Missing " + RIGHT_DIV);
                }

                rightChunk = template.Substring(startOfmRightTemplate);

            }
            catch (Exception ex)
            {
                throw new ApplicationException("Templating failed! " + ex.Message);
            }
        }


          public static string LoadMapMvc(System.Collections.Specialized.NameValueCollection query, string heading, string layer)
          {
              string place = query.Count == 0 ? "Laos" : query[0];
   
              string busStopId = query["busstop"];

              var sbret = new StringBuilder();
              sbret.Append(MapScriptBuilder.BuildMapContentMvc(place, heading, layer, busStopId));
              return sbret.ToString();
          }

        public static void LoadMap(System.Web.UI.Page page, string heading)
        {
            try
            {
                string leftChunk;
                string rightChunk;
                Load2(page, out leftChunk, out rightChunk);

                var sbret = new StringBuilder();
                sbret.Append(leftChunk);
                sbret.Append(MapScriptBuilder.BuildMapContent(page.ClientQueryString, heading));
                sbret.Append(rightChunk);
                page.Response.Write(sbret);
            }
            catch (Exception ex)
            {
                page.Response.Write("Load Map failed! " + HTMLIse(ex.Message));
            }
        }

        private static string GetFile(string localFileName, System.Web.UI.Page page)
        {
            string sz1 = page.Server.MapPath(localFileName);

            using (TextReader tr = new StreamReader(sz1, Encoding.ASCII))
            {
                string contents = tr.ReadToEnd();
                return contents;
            }
        }
    }
}